package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.impl.ArticleReplyServiceImpl;
import com.example.forum.services.impl.ArticleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-09-26
 * Time: 11:28
 */
@Api(tags = "帖子回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private ArticleReplyServiceImpl articleReplyService;
    @Resource
    private ArticleServiceImpl articleService;

    /**
     * 新增帖子回复
     *
     * @param articleId 帖子id
     * @param content   回复正文
     * @return
     */
    @ApiOperation("新增回复")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("回复正文") @RequestParam("content") @NonNull String content) {
        // 禁止正文以空格开头(因为前端已经trim()了,因此空格开头一定是非法的,直接禁用即可)
        if (content.charAt(0) == ' ') {
            // 打印日志
            log.warn("检测到不合规范的 帖子回复 提交 --- 回复正文以空格开头");
            // 返回结果
            return AppResult.fail(ResultCode.FAILED);
        }
        // 获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION_KEY);
        // 根据用户传入的 articleId 查询对应帖子
        Article article = articleService.selectById(articleId);
        // 验证帖子状态
        if (article == null || article.getDeleteState() != 0) {
            // 打印日志
            log.warn("检测到用户尝试对不存在的帖子发布评论,article id = " + articleId + ",user id = " + user.getId());
            // 返回结果
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (article.getState() != 0) {
            // 打印日志
            log.warn("检测到用户尝试对被封贴的帖子发布评论,article id = " + articleId + ",user id = " + user.getId());
            // 返回结果
            return AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED);
        }
        // 创建 articleReply 对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        // 调用 service 层
        articleReplyService.create(articleReply);
        // 返回结果
        return AppResult.success();
    }

    @ApiOperation("获取帖子的回复列表")
    @GetMapping("/getReplies")
    public AppResult getRepliesByArticleId(@ApiParam("帖子id") @RequestParam("articleId") @NonNull Long articleId) {
        // 根据帖子 id 查询对应帖子
        Article article = articleService.selectById(articleId);
        // 验证帖子
        if (article == null || article.getDeleteState() != 0) {
            // 打印日志
            log.warn("检测到用户尝试获取不存在或已被删除的帖子的回复列表,article id = " + articleId);
            // 返回结果
            return AppResult.fail(ResultCode.FAILED);
        }
        // 调用 service,获取帖子的回复列表
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        if (articleReplies == null) {
            articleReplies = new ArrayList<>();
        }
        return AppResult.success(articleReplies);
    }
}
